package com.project.controller.web;

import com.alibaba.fastjson.JSONObject;
import com.project.entity.School;
import com.project.myBeans.ResponseBean;
import com.project.service.SchoolManagerInfoService;
import com.project.service.SchoolService;
import com.project.service.base.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by WheelChen on 2017/6/21.
 * <p>
 * 学院相关操作
 */
@Controller
@RequestMapping("/school")
public class SchoolController extends BaseService {
    @Autowired
    SchoolService schoolService;

    @Autowired
    private SchoolManagerInfoService schoolManagerInfoService;


    /**
     * author: WheelChen
     * date: 2017-6-22
     * function: 根据学校id查找学院
     *
     * @param universityId 学校id
     * @return {json数组} schools
     */
    @RequestMapping(value = "/findBath/{universityId}", method = RequestMethod.GET)
    @ResponseBody
    public ResponseBean findBath(@PathVariable("universityId") Integer universityId) {
        ResponseBean responseBean = new ResponseBean();
        List<School> schoolList;
        try {
            schoolList = schoolService.findBatchByUniversityId(universityId);
            responseBean.setData(schoolList);
            responseBean.setSuccess(true);
            return responseBean;
        } catch (Exception e) {
            responseBean.setSuccess(false);
            return responseBean;
        }
    }
    
    @RequestMapping(value = "/findBatchByUniversityIdWithAdmin/{universityId}", method = RequestMethod.GET)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/9/18
     * function: 根据学校id查找学院以及学院的管理员
     *
     * @param universityId
     * @return
     */
    public ResponseBean findBatchByUniversityIdWithAdmin(@PathVariable("universityId") Integer universityId){
        ResponseBean responseBean = new ResponseBean();
        List<JSONObject> jsonObjectList = new ArrayList<>();
        List<School> schoolList;
        try {
            schoolList = schoolService.findBatchByUniversityId(universityId);
            for (School school: schoolList){
                JSONObject jsonObject = new JSONObject();
                //学院信息
                jsonObject.put("school", school);
                //学院管理员信息
                if (school.getId() != null)
                jsonObject.put("schoolManagerInfo",schoolManagerInfoService.findOneBySchoolId(school.getId()));
                jsonObjectList.add(jsonObject);
            }
            responseBean.setData(jsonObjectList);
            responseBean.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
        }
        return responseBean;
    }

    @RequestMapping(value = "/addOne", method = RequestMethod.POST)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/7/11
     * function: 添加学院
     *
     * @param school
     * @return
     */
    public ResponseBean addOne(@RequestBody School school) {
        ResponseBean responseBean = new ResponseBean();
        try {
            //查看学院是否已存在
            if (schoolService.checkUniversityIdAndNameIsExist(school)) {
                responseBean.setSuccess(false);
                responseBean.setMessage("已存在，创建失败");
            } else {
                schoolService.add(school);
                responseBean.setSuccess(true);
                responseBean.setMessage("创建成功");
            }
        } catch (Exception e) {
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
        }
        return responseBean;
    }

}
